package com.zhiwang.planet.mvp.p

import com.zhiwang.planet.BuildConfig
import com.zhiwang.planet.di.ActivityScope
import com.zhiwang.planet.mvp.v.GameListView
import com.zhiwang.planet.mvp.v.KmView
import com.zhiwang.planet.mvp.v.RewardListView
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.mvvm.model.request.KmRequest
import com.zhiwang.planet.mvvm.model.request.PageIndexRequest
import com.uber.autodispose.AutoDispose.autoDisposable
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
import com.zhiwang.planet.mvvm.model.request.WelfareExchange
import com.zhiwang.planet.util.*
import com.zhiwang.planet.util.UrlUtil.baseUrl
import com.zhiwang.planet.util.UrlUtil.ji7BaseUrl
import javax.inject.Inject

/**
 *
 */
@ActivityScope
class ScalarPresenter @Inject constructor(var restApi: RestApi, var scopeProvider: AndroidLifecycleScopeProvider) {


    lateinit var rewardListView: RewardListView
    lateinit var kmView: KmView
    var gameListView: GameListView? = null

    fun getRewardList(page: Int) {
        restApi.getRewardList(PageIndexRequest(page, type = "jp"))
                .androidSchedule()
                .responseFunc()
                .`as`(autoDisposable(scopeProvider))
                .subscribe({
                    rewardListView.onLoadReward(it)
                }, {
                    rewardListView.onError(it)
                }, {

                })
    }


    fun getBonusCardList(page: Int) {

        restApi.getBonusCardList(PageIndexRequest(page, type = "flk"))
                .androidSchedule()
                .responseFunc()
                .`as`(autoDisposable(scopeProvider))
                .subscribe({
                    rewardListView.onLoadReward(it)
                }, {
                    rewardListView.onError(it)
                }, {

                })

    }

    fun getKmInfo(kmId: String, pwd: String) {
        restApi.getKmInfo(KmRequest(kmId, pwd))
                .androidSchedule()
                .responseFunc()
                .`as`(autoDisposable(scopeProvider))
                .subscribe({
                    kmView.onLoadKmInfo(it)
                }, {
                    kmView.onError(it)
                }, {

                })
    }

    fun getCardInfo(kmId: String, pwd: String) {
        restApi.getCardInfo(KmRequest(kmId, pwd))
                .androidSchedule()
                .responseFunc()
                .`as`(autoDisposable(scopeProvider))
                .subscribe({
                    kmView.onLoadKmInfo(it)
                }, {
                    kmView.onError(it)
                }, {

                })

    }


    fun getGameList() {
        restApi.getGameList(gameListUrl())
                .androidSchedule()
                .responseFunc()
                .`as`(autoDisposable(scopeProvider))
                .subscribe({
                    gameListView?.onGameListLoad(it)
                }, {

                    gameListView?.onError(it)
                }, {

                })

    }

    fun gameListUrl():String{

        if (BuildConfig.DEBUG){
            if (AppHelper.testEnv())
                return BuildConfig.DEBUG_Ji7_APP_BASE_URL + "index/jcgame_list"
        }
        return BuildConfig.RELEASE_Ji7_APP_BASE_URL + "index/jcgame_list"

    }


    //兑换福利卡
    fun exchange(userid: String, friendId: String, cardsList: String, onMsg: (String) -> Unit, onError: (Throwable) -> Unit) {
        restApi.exchangeCards(ji7BaseUrl() + "dshop/use_fulika_multi", WelfareExchange(friendId, userid, cardsList))
                .androidSchedule()
                .autoDispose(scopeProvider)
                .subscribe({
                    if (it.errCode == 0) {
                        onMsg(it.errMsg)
                    } else {
                        onError(Throwable(it.errMsg))
                    }
                }, {
                    onMsg(it.localizedMessage)
                }, {

                })

    }


    fun verifyCard(cardNumber: String, onMsg: (String) -> Unit, onError: (Throwable) -> Unit) {

        restApi
                .verify(ji7BaseUrl() + "dshop/search_km_multi", cardNumber)
                .androidSchedule()
                .autoDispose(scopeProvider)
                .subscribe({
                    if (it.errCode == 0) {
                        onMsg(it.errMsg)
                    } else {
                        onError(Throwable(it.errMsg))
                    }
                }, {
                    onMsg(it.localizedMessage)
                }, {

                })


    }

    fun transformCard(cardNumber: String, onMsg: (String) -> Unit, onError: (Throwable) -> Unit) {

        restApi
                .transformCard(ji7BaseUrl() + "dshop/transformation_km_multi", cardNumber)
                .androidSchedule()
                .autoDispose(scopeProvider)
                .subscribe({
                    if (it.errCode == 0) {
                        onMsg(it.errMsg)
                    } else {
                        onError(Throwable(it.errMsg))
                    }
                }, {
                    onMsg(it.localizedMessage)
                }, {

                })


    }

    fun checkUserExists(userId: String, onError: (Throwable) -> Unit) {
        restApi.checkUserExists(ji7BaseUrl() + "dshop/find_user", userId)
                .androidSchedule()
                .autoDispose(scopeProvider)
                .subscribe({
                    if (it.errCode != 0)
                        onError(Throwable(it.errMsg))
                }, {

                }, {

                })


    }

}